home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / scene.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.7 KB  |  336 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* scene.c - open a window and clear the background.
  19.  *    Set up callbacks to handle keyboard input.
  20.  *      Model some objects.  Use perspective projection.
  21.  *    Use independent modeling transformations to position objects.
  22.  *
  23.  *    F1 key            - print help information
  24.  *    SPACE key        - generates a random background color
  25.  *    <s> key            - toggle smooth/flat shading
  26.  *    Escape Key        - exit program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <stdio.h>
  33. #include <math.h>
  34.  
  35. /* Function Prototypes */
  36.  
  37. GLvoid initgfx( GLvoid );
  38. GLvoid keyboard( GLubyte, GLint, GLint );
  39. GLvoid specialkeys( GLint, GLint, GLint );
  40. GLvoid drawScene( GLvoid );
  41.  
  42. void checkError( char * );
  43. void printHelp( char * );
  44.  
  45. /* Global Variables */
  46.  
  47. static char *progname; 
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. void
  54. main( int argc, char *argv[] )
  55. {
  56.     GLsizei width, height;
  57.  
  58.     glutInit( &argc, argv );
  59.  
  60.     /* create a window that is 1/4 the size of the screen,
  61.      * and position it in the middle of the screen.
  62.      */
  63.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  64.     height = glutGet( GLUT_SCREEN_HEIGHT );
  65.     glutInitWindowPosition( width / 4, height / 4 );
  66.     glutInitWindowSize( width / 2, height / 2 );
  67.     glutInitDisplayMode( GLUT_RGBA );
  68.     glutCreateWindow( argv[0] );
  69.  
  70.     initgfx();
  71.  
  72.     glutKeyboardFunc( keyboard );
  73.     glutSpecialFunc( specialkeys );
  74.     glutDisplayFunc( drawScene ); 
  75.  
  76.     progname = argv[0];
  77.  
  78.     printHelp( progname );
  79.  
  80.     glutMainLoop();
  81. }
  82.  
  83. void
  84. printHelp( char *progname )
  85. {
  86.     fprintf(stdout, 
  87.         "\n%s - create a scene using independent models\n\n"
  88.         "F1 key        - print help information\n"
  89.         "SPACE Key    - generates a random background color\n"
  90.         "<s> key        - toggle smooth/flat shading\n"
  91.         "Escape Key    - exit the program\n\n",
  92.         progname);
  93. }
  94.  
  95. GLvoid
  96. initgfx( GLvoid )
  97. {
  98.     /* set clear color to blue */
  99.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  100.  
  101.     glMatrixMode( GL_PROJECTION );
  102.     gluPerspective( 45.0, 1.0, 3.0, 7.0 );
  103.     glMatrixMode( GL_MODELVIEW );
  104. }
  105.  
  106. void 
  107. checkError( char *label )
  108. {
  109.     GLenum error;
  110.     while ( (error = glGetError()) != GL_NO_ERROR )
  111.         printf( "%s: %s\n", label, gluErrorString(error) );
  112. }
  113.  
  114. GLvoid 
  115. keyboard( GLubyte key, GLint x, GLint y )
  116. {
  117.     static GLboolean flat = GL_FALSE;
  118.  
  119.     switch (key) {
  120.     case ' ':    /* SPACE key */
  121.         /* generate a random background color */
  122.         glClearColor( drand48(), drand48(), drand48(), 1.0 ); 
  123.         glutPostRedisplay();
  124.         break;
  125.     case 's':    /* s key */
  126.         /* toggle between smooth and flat shading */
  127.         flat = !flat;
  128.         if (flat)
  129.             glShadeModel( GL_FLAT );
  130.         else 
  131.             glShadeModel( GL_SMOOTH );
  132.         glutPostRedisplay();
  133.         break;
  134.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  135.         exit(0);
  136.     }
  137. }
  138.  
  139. GLvoid 
  140. specialkeys( GLint key, GLint x, GLint y )
  141. {
  142.     switch (key) {
  143.     case GLUT_KEY_F1:    /* Function key #1 */
  144.         /* print help information */
  145.         printHelp( progname );
  146.         break;
  147.     }
  148. }
  149.  
  150. GLvoid
  151. drawWindow()
  152. {
  153.     /* window */
  154.     static GLfloat v0[] = { 0.0, 0.4 };
  155.     static GLfloat v1[] = { 0.0, 0.0 };
  156.     static GLfloat v2[] = { 0.1, 0.4 };
  157.     static GLfloat v3[] = { 0.1, 0.0 };
  158.     static GLfloat v4[] = { 0.2, 0.4 };
  159.     static GLfloat v5[] = { 0.2, 0.0 };
  160.  
  161.     /* draw 2 quadrilateral strip to make a window */
  162.     glBegin( GL_QUAD_STRIP );
  163.         glColor3f( 1.0, 0.0, 0.0 );
  164.         glVertex2fv (v0);
  165.         glColor3f( 0.9, 0.0, 1.0 );
  166.         glVertex2fv (v1);
  167.         glColor3f( 0.8, 0.1, 0.0 );
  168.         glVertex2fv (v2);
  169.         glColor3f( 0.7, 0.2, 1.0 );
  170.         glVertex2fv (v3);
  171.         glColor3f( 0.6, 0.3, 0.0 );
  172.         glVertex2fv (v4);
  173.         glColor3f( 0.5, 0.4, 1.0 );
  174.         glVertex2fv (v5);
  175.         glColor3f( 0.4, 0.5, 0.0 );
  176.     glEnd();
  177. }
  178.  
  179. /* draw a "circle" using a triangle fan; set the
  180.  * center of the circle to white, and the outside
  181.  * to the color passed in
  182.  */
  183. GLvoid
  184. drawFan( GLfloat *color )
  185. {
  186.     /* Draw a triangle fan centered a the current coordinate
  187.      * system origin 
  188.      */
  189.     glBegin( GL_TRIANGLE_FAN );
  190.         glColor3f( 1.0, 1.0, 1.0 );
  191.         glVertex2f( 0.0, 0.0 );
  192.         glColor3fv( color );
  193.         glVertex2f( 0.0, -0.2 );
  194.         glVertex2f( 0.2, -0.1 );
  195.         glVertex2f( 0.2, 0.1 );
  196.         glVertex2f( 0.0, 0.2 );
  197.         glVertex2f( -0.2, 0.1 );
  198.         glVertex2f( -0.2, -0.1 );
  199.         glVertex2f( 0.0, -0.2 );
  200.     glEnd();
  201. }
  202.  
  203. /* draw a flower with the base of the stem 
  204.  * at the current location 
  205.  */
  206. GLvoid
  207. drawFlower( GLfloat *color )
  208. {
  209.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  210.     /* draw the stem with 2 leaves */
  211.  
  212.     glColor3fv( darkgreen );
  213.     glBegin( GL_LINES );
  214.         glVertex2f( 0.0, 0.0 );         /* stem */
  215.         glVertex2f( 0.0, 0.25 );
  216.         glVertex2f( 0.0, 0.1 );         /* leaf */
  217.         glVertex2f( 0.05, 0.15 ); 
  218.         glVertex2f( 0.0, 0.05 );     /* leaf */
  219.         glVertex2f( -0.05, 0.2 );
  220.     glEnd();
  221.  
  222.     glPushMatrix();
  223.         /* move to the top of the stem */
  224.         glTranslatef( 0.0, 0.25, 0.0 );
  225.  
  226.         /* use a scaled triangle fan for the flower head */
  227.         glScalef( 0.1, 0.1, 1.0 );
  228.         drawFan( color );  
  229.     glPopMatrix();
  230. }
  231.  
  232. /* draw a house centered at the current origin;
  233.  * the bounding box for the house is 1.0 x 2.0 units
  234.  */
  235. GLvoid
  236. drawHouse()
  237. {
  238.     /* draw the house */
  239.     glColor3f( 1.0, 1.0, 1.0 ); /* white */
  240.     glRectf( -0.5, -0.75, 0.5, 0.75 );
  241.  
  242.     /* draw the door */
  243.     glColor3f( 0.5, 0.2, 0.1 ); /* brown */
  244.     glRectf( -0.2, -0.75, 0.2, 0.0 );
  245.  
  246.     glPushMatrix();
  247.         /* move to the top left corner of the house */
  248.         glTranslatef( -0.5, 0.75, 0.0 );
  249.  
  250.         /* draw a triangle for the roof */
  251.         glColor3f( 0.0, 0.0, 0.0 ); /* black */
  252.         glBegin( GL_TRIANGLES );
  253.             glVertex2f( 0.0, 0.0 );
  254.             glVertex2f( 1.0, 0.0 );
  255.             glVertex2f( 0.5, 0.5 );
  256.         glEnd();
  257.     glPopMatrix();
  258.  
  259.     glPushMatrix();
  260.         /* move to the location for the left window */
  261.         glTranslatef( -0.4, 0.2, 0.0 );
  262.         drawWindow();
  263.     glPopMatrix();
  264.  
  265.     glPushMatrix();
  266.         /* move to the location for the right window */
  267.         glTranslatef( 0.2, 0.2, 0.0 );
  268.         drawWindow();
  269.     glPopMatrix();
  270. }
  271.  
  272. GLvoid
  273. drawScene( GLvoid )
  274. {
  275.     static GLfloat red[] = { 1.0, 0.0, 0.0 };
  276.     static GLfloat magenta[] = { 1.0, 0.0, 1.0 };
  277.     static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
  278.     static GLfloat blue[] = { 0.0, 0.0, 1.0 };
  279.     static GLfloat green[] = { 0.0, 1.0, 0.0 };
  280.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  281.  
  282.     glClear( GL_COLOR_BUFFER_BIT );
  283.  
  284.     glPushMatrix();
  285.  
  286.         /* move inside the viewing volume */
  287.         glTranslatef( 0.0, 0.0, -4.0 );
  288.  
  289.         /* draw the ground in different shades of green */
  290.         glBegin( GL_POLYGON );
  291.             glColor3fv( green );
  292.             glVertex2f( -2.0, -2.0 );
  293.             glVertex2f( 2.0, -2.0 );
  294.             glColor3fv( darkgreen );
  295.             glVertex2f( 2.0, 0.0 );
  296.             glVertex2f( -2.0, 0.0 );
  297.         glEnd();
  298.  
  299.         /* draw the house */
  300.         glPushMatrix();
  301.             /* move to where the center of the house should be */
  302.             glTranslatef( -0.5, 0.0, 0.0 );
  303.  
  304.             /* shrink the house slightly */
  305.             glScalef( 0.7, 0.7, 0.7 );
  306.             drawHouse();
  307.         glPopMatrix();
  308.  
  309.         /* draw the sun */
  310.         glPushMatrix();
  311.             /* move to the location of the sun */
  312.             glTranslatef( 2.0, 2.0, -1.5 );
  313.             glColor3fv( yellow );
  314.             glutSolidSphere( 0.2, 8, 15 );
  315.         glPopMatrix();
  316.  
  317.         /* draw several tulips in the foreground */
  318.         glPushMatrix();
  319.             glTranslatef( 1.0, -1.0, 1.0 );
  320.             drawFlower( red );
  321.             glTranslatef( 0.0, 0.0, -0.5 );
  322.             drawFlower( yellow );
  323.         glPopMatrix();
  324.         glPushMatrix();
  325.             glTranslatef( -1.0, -1.0, 1.0 );
  326.             drawFlower( blue );
  327.             glTranslatef( 0.0, 0.0, -0.5 );
  328.             drawFlower( magenta );
  329.         glPopMatrix();
  330.  
  331.     glPopMatrix();
  332.  
  333.     checkError( "drawScene" );
  334.     glFlush();
  335. }
  336.